package where2go.sctu.com.zhiyou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.TypeTwoViewHolder;
import where2go.sctu.com.zhiyou.entity.PersonRecomClass;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.adpater
 * Created by 张某人
 * CreatedTime:on 2018/5/1   21:20
 * Description:
 */

public class RecyclerViewTwoAdapter extends RecyclerView.Adapter<TypeTwoViewHolder> {

    //private LayoutInflater mLayoutInflater;

    private String urlPersonal="http://47.104.80.155/youyiyou/resources/classImg/";
    private List<PersonRecomClass.DataBean> mList=new ArrayList<>();
    private Context context;
   // private RequestQueue queues;  //利用Volley加载网络图片用到

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }

    private RecyclerViewTwoAdapter.OnItemClickListener mOnItemClickListener;

    public void setItemClickListener(RecyclerViewTwoAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener=listener;
    }


    public RecyclerViewTwoAdapter(List<PersonRecomClass.DataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    public void addList(List<PersonRecomClass.DataBean> list){
        mList.addAll(list);
    }

    @Override
    public TypeTwoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=LayoutInflater.from(context).inflate(R.layout.item_type_two,parent,false);
        TypeTwoViewHolder holder=new TypeTwoViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(final TypeTwoViewHolder holder, final int position) {
            PersonRecomClass.DataBean personBean=mList.get(position);
            holder.name.setText(personBean.getClassTitle());
            holder.content.setText(personBean.getClassDetail());
            ImageRequest imageRequest = new ImageRequest(urlPersonal+mList.get(position).getClassImg(), new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                holder.avatar.setImageBitmap(bitmap);
            }
        }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //请求失败 设置默认图片
                    holder.avatar.setImageResource(R.mipmap.ic_launcher);
                }
        });
        MyApplication.getHttpQueues().add(imageRequest);

        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemLongClick(holder.itemView,position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList !=null ? mList.size():0;
    }
}
